package cn.mumzone.pos.presenter;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.basic.util.log.Elog;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.event.JPushEvent;
import cn.mumzone.pos.model.CashServiceResult;
import cn.mumzone.pos.model.source.ReserveRepository;
import cn.mumzone.pos.presenter.contract.CashServiceContract;
import rx.Scheduler;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Created by qiuzhenhuan on 2019/7/5.
 */

public class CashServicePresenter extends AbsPresenter<CashServiceContract.View, ReserveRepository> implements CashServiceContract.Presenter {

    private String status;
    private String keyWord;

    private void loadAppointmentList(String status, String keyWord, String orderCode) {

        ApiCallBack<CashServiceResult> callBack = new ApiCallBack<CashServiceResult>() {
            @Override
            public void onSuccess(CashServiceResult cashServiceResult) {
                mView.showAppointmentInfo(cashServiceResult);
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }

            @Override
            public void onNetworkError(BaseResponse response) {
                mView.showNetError();
            }

        };
        mRepo.getAppointmentList(status, keyWord, StringUtils.get(orderCode), callBack);
        addSubscrebe(callBack);
    }

    @Override
    public void refreshAppointmentList() {
        loadAppointmentList(status, keyWord, null);
    }

    @Override
    public void refreshAppointmentListWithKeyWord(String keyWord) {
        if (!StringUtils.isEmpty(this.keyWord) && this.keyWord.equals(keyWord)) {
            return;
        }
        this.keyWord = keyWord;
        loadAppointmentList(status, keyWord, null);
    }


    public void setStatus(String status) {
        this.status = status;
    }


    @Override
    public void initEvent() {
        Subscription jpushReceiver = RxBus.getDefault()
                .toObservable(JPushEvent.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<JPushEvent>() {
                    @Override
                    public void call(JPushEvent jPushEvent) {
                        Elog.e("JPush", "已处理消息");
                        mView.showJPushView();
                    }
                });
        addSubscrebe(jpushReceiver);
    }

    @Override
    public void searchByScan(String orderCode) {
        loadAppointmentList("", "", orderCode);
    }
}
